<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class DownloadLog extends Model
{
    //
    use SoftDeletes;
    protected $table = 'download_log';

    /**
     * 定义主键
     * @var string
     */
    protected $primaryKey = 'id';

    /**
     * 禁止批量赋值的字段
     *
     * @var array
     */
    protected $guarded = ['id','created_at','deleted_at'];

    /**
     * 软删除应该被调整为日期的属性
     * [$dates description]
     * @var [type]
     */
    protected $dates=[
        'created_at', 'updated_at','deleted_at'
    ];

    /*
     * 存储数据
     */
    public static function store($version_id,$download_url){

        //$ip = request() -> ip();
        $ip = request() -> getClientIp();
        $count = self::where('ip',$ip) ->where('version_id',$version_id) -> whereRaw('UNIX_TIMESTAMP(created_at) >'.(time() - 600)) -> count();
        if($count >= 5){
            return ['error' => 4032];
        }

        $params = [
            'link_url' => $download_url,
            'ip' => $ip,
            'version_id' => $version_id
        ];
        return self::create($params);



    }
}
